﻿using System;
using System.Web.UI.WebControls;
using Business;
using EntitiesTM;

namespace TimeWork
{
    public partial class ResultUnit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                lblMissName.Visible = false;
                FillGrid();
            }
        }

        public void FillGrid()
        {
            GVListResultUnit.DataSource = BuResultUnit.selAllResultUnit(Session["companyid"].ToString());
            GVListResultUnit.DataBind();
        }

        protected void GVListResultUnit_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVListResultUnit.EditIndex = e.NewEditIndex;
            FillGrid();
        }

        protected void GVListResultUnit_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            EnResultUnit resultUnit = new EnResultUnit();
            resultUnit.resultUnitName = ((TextBox)GVListResultUnit.Rows[e.RowIndex].FindControl("txtName")).Text;
            resultUnit.resultUnitDescription = ((TextBox)GVListResultUnit.Rows[e.RowIndex].FindControl("txtDes")).Text;
            resultUnit.resultUnitID = Int32.Parse(GVListResultUnit.DataKeys[e.RowIndex].Values[0].ToString());
            resultUnit.resultUnitNameAlt = ((TextBox)GVListResultUnit.Rows[e.RowIndex].FindControl("txtCode")).Text;
            BuResultUnit.updResultUnit(resultUnit, Session["companyid"].ToString());

            GVListResultUnit.EditIndex = -1;
            FillGrid();
        }

        protected void GVListResultUnit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVListResultUnit.EditIndex = -1;
            FillGrid();
        }

        protected void GVListResultUnit_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GVListResultUnit_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            EnResultUnit resultUnit = new EnResultUnit();
            resultUnit.resultUnitID = Int32.Parse(GVListResultUnit.DataKeys[e.RowIndex].Values[0].ToString());
            BuResultUnit.delResultUnit(resultUnit, Session["companyid"].ToString());
            FillGrid();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            lblMissName.Visible = false;
            string name = txtResultUnitName.Text.Trim();
            if (name.Equals(""))
            {
                lblMissName.Visible = true;
            }
            else
            {
                EnResultUnit resultUnit = new EnResultUnit();
                resultUnit.resultUnitName = name;
                resultUnit.resultUnitDescription = txtResultUnitDescription.Text.Trim();
                resultUnit.resultUnitNameAlt = txtResultUnitNameAlt.Text.Trim();
                BuResultUnit.insResultUnit(resultUnit, Session["companyid"].ToString());
                FillGrid();
                txtResultUnitName.Text = String.Empty;
                txtResultUnitDescription.Text = String.Empty;
                txtResultUnitNameAlt.Text = String.Empty;
            }
        }
    }
}